﻿using System;
using System.Collections;
using System.Collections.Generic;

using MySql.Data.MySqlClient; 

using UnityEngine;

public class MySqlTools 
{
    string _server, _port,_user, _password, _datename;
    public MySqlTools(string host,string port,string user,string passwd,string database)
    {
        _server = host;
        _port = port;_user = user;
        _password = passwd;
        _datename = database;
    }
  
    public List<CulQuestionData> Search(string table)//查询，读取数据
    {
        List<CulQuestionData> questionDatas = new List<CulQuestionData>();
        string connectStr = string.Format("server={0};port={1};user={2};password={3}; database={4};charset=utf8", _server, _port, _user, _password, _datename);//设置连接ip，端口，用户名，密码，以及编码格式 
        MySqlConnection conn = new MySqlConnection(connectStr);//创建连接类
        conn.Open();//正式打开连接 

        string read_sql = "select * from party_building";//sql命令，选择user1表
        MySqlCommand read_cmd = new MySqlCommand(read_sql, conn);
        MySqlDataReader reader = read_cmd.ExecuteReader();

        while (reader.Read())//Read一次就是一行数据，Read不为空执行打印数据
        {//new List<string> { (string)reader[3], (string)reader[4], (string)reader[5], (string)reader[6] }
            List<string> answer = new List<string>();
            if ((string)reader[3] != "null")
                answer.Add((string)reader[3]);
            if ((string)reader[4] != "null")
                answer.Add((string)reader[4]);
            if ((string)reader[5] != "null")
                answer.Add((string)reader[5]);
            if ((string)reader[6] != "null")
                answer.Add((string)reader[6]);
            CulQuestionData data = new CulQuestionData((int)reader[0], (E_QuestionType)reader[2], (string)reader[1], answer, (string)reader[7]);
            questionDatas.Add(data);
        }
        conn.Close();//关闭连接
        return questionDatas;
    }
}
class ConnDB
{
    //定义连接地址、端口号、登录用户名、密码、数据库名
    private string server;
    private string port;
    private string user;
    private string password;
    private string datename;

    private MySqlConnection conn;
    //构造函数接收参数
    public ConnDB(string _server, string _port, string _user, string _password, string _datename)
    {
        this.server = _server;
        this.port = _port;
        this.user = _user;
        this.password = _password;
        this.datename = _datename;
    }
    // 
    /// <summary>
    ///  连接打开数据库
    /// </summary>
    public MySqlConnection openDate()
    {
        try
        {
            string connStr = string.Format("server={0};port={1};user={2};password={3}; database={4};", server, port, user, password, datename);
            //连接数据库
            conn = new MySqlConnection(connStr);
            //打开
            conn.Open();
        }
        catch (Exception e)
        {
            Console.WriteLine("连接数据库错误,原因为：" + e.ToString());
        }
        return conn;
    }
    public void closeDB()
    {
        //关闭数据库
        conn.Close();
    }
}
